import {
    Component,
    OnInit,
    ViewEncapsulation,
    Injector,
    ViewChild,
} from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    PipelineNodeServiceProxy,
} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';

@Component({
    templateUrl: './outplan.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()],
})
export class OutplanComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    advancedFiltersAreShown = false;
    filterText = '';
    startState = '';
    isDelivery = '';
    isReceived = false;
    selectedpipe: Array<number>;
    tabs = [];

    constructor(
        injector: Injector,
        private _pipelineNodesService: PipelineNodeServiceProxy
    ) {
        super(injector);
    }

    ngOnInit(): void {}

    tabTypeClick(isReceived: boolean) {
        this.isReceived = isReceived;

        //this.getDeliveryRecords();
    }

    getDeliveryRecords(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);

            return;
        }

        this.primengTableHelper.showLoadingIndicator();

        this._pipelineNodesService
            .getOutList(
                this.startState,
                this.isDelivery,
                this.filterText,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(
                    this.paginator,
                    event
                ),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .pipe(
                finalize(() => this.primengTableHelper.hideLoadingIndicator())
            )
            .subscribe((result) => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
}
